package com.example.dllo.my36kr.ui.adapter;

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.dllo.my36kr.R;
import com.example.dllo.my36kr.model.MineBean;

import java.util.ArrayList;

/**
 * Created by CPC_272 on 16/8/18.
 */
public class MineAdapter extends BaseAdapter {
    private ArrayList<MineBean>data;
    private Context context;
    //TYPE不可以随意设置 保证TYPE类型必须为整数并且不小于TYPE_count
    final int TYPE_1 = 0; // 代表第一种行布局的类型
    final int TYPE_2 = 1; // 代表第二种行布局的类型
    final int TYPE_COUNT= 2; // 代表一共有多少种行布局类型
    public MineAdapter(Context context) {
        this.context = context;
    }

    public void setData(ArrayList<MineBean> data) {
        this.data = data;
        notifyDataSetChanged();
    }


    @Override
    public int getCount() {
        return data.size();
    }

    @Override
    public Object getItem(int position) {
        return data.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }
    /**
     * 此方法获取行布局类型
     * @param position
     * @return
     */
    @Override
    public int getItemViewType(int position) {
        if (position == 0 || position == 2 || position == 5 || position == 10 || position == 12 ||position == 14) {
            return TYPE_2;
        } else {
            return TYPE_1;
        }
    }
    /**
     * 获取行视图类型的数量
     * @return
     */
    @Override
    public int getViewTypeCount() {
        return TYPE_COUNT;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder=null;
        EmptHolder emptHolder=null;
        // 定义一个type 获取行视图的类型 通过类型来加载不同的行布局
        int type = getItemViewType(position);
        if (convertView == null) {
            switch (type){
                case TYPE_1:
                    convertView= LayoutInflater.from(context).inflate(R.layout.fragment_mine_item,parent,false);
                    holder=new ViewHolder(convertView);
                    convertView.setTag(holder);
                    break;
                case TYPE_2:
                    convertView= LayoutInflater.from(context).inflate(R.layout.item_mine_empt,parent,false);
                    emptHolder= new EmptHolder(convertView);
                    convertView.setTag(emptHolder);
                    break;
            }
        }else{
            switch (type) {
                case TYPE_1:
                    holder = (ViewHolder) convertView.getTag();
                    break;
                case TYPE_2:
                    emptHolder = (EmptHolder) convertView.getTag();
                    break;
            }
        }
        switch (type) {
            case TYPE_1:
                holder.im.setImageResource(data.get(position).getImage());
                holder.tv.setText(data.get(position).getTitle());
                break;
            case TYPE_2:
                emptHolder.ll.setBackgroundColor(Color.GRAY);
               break;
        }
        return convertView;
    }

    class ViewHolder{
        private TextView tv;
        private ImageView im;
        public ViewHolder(View view) {
            tv = (TextView) view.findViewById(R.id.tv_item_mine);
            im = (ImageView) view.findViewById(R.id.image_item_mine);
        }
    }
    class EmptHolder{
        //加载线性布局为了让这块显示为灰色 布局里面没写东西 就把线性布局背景设为灰色
          private LinearLayout ll;
        public EmptHolder(View view) {
            ll = (LinearLayout) view.findViewById(R.id.ll_mine);
        }
    }
}
